package com.gitee.lw114514.test_spring_1;

import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
public class ResponseController {

    @RequestMapping("/response")
    public void response(HttpServletResponse response) {
        System.out.println("这是响应");
        //设置响应状态码
        response.setStatus(200);

        //设置响应头
        response.setHeader("Content-Type", "text/html");
        response.setHeader("Cache-Control", "no-cache");

        //设置响应体
        try {
            response.getWriter().write("<h1>This is the response body</h1>");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @RequestMapping("/response2")
    public ResponseEntity<String> response2() {
        return (ResponseEntity<String>) ResponseEntity.ok("<h1>This is the response body</h1>").status(200).header("Content-Type", "text/html");
    }
}
